package cn.com.zsc.cm;

import java.util.Hashtable;
import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.zsc.util.ConstValue;

import com.jarp.db.DAO;

/**
 * 成本报表类
 * @author 杨景
 *
 */
public class IntegrateCost {
	/**
	 * 获取成本列表
	 * 
	 * @param start
	 * @param limit
	 * @param cType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getCost() {
		String sql = "select " +
							"b.CL_NAME AS SHOW_TYPE, " + 
							"c.CL_NAME AS SHOW_CTYPE, " + 
							"d.NAME as SHOW_EP, " + 
							"a.* "
					 + "from CTM_COST a " + 
					 		"left join CM_COMMLIST b on a.TYPE=b.CL_CODE " + 
					 		"left join CM_COMMLIST c on a.CTYPE=c.CL_CODE " + 
					 		"left join APP_LOGIN d on a.ep=d.uid " + 
					 		"order by a.ID desc";

		DAO dao = new DAO();

		String resultStr = ConstValue.EMPTY_RECORD_NOT_PAGEING;

		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null && v.size() > 0) {
				JSONArray json = JSONArray.fromObject(v);

				resultStr = json.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ConstValue.EMPTY_RECORD_NOT_PAGEING;
		}

		return resultStr;
	}

	/**
	 * 获取成本列表(带查询条件)
	 * 
	 * @param start
	 * @param limit
	 * @param cType
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getCost(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);

		String startDate = jsobject.getString("startDate");
		String endDate = jsobject.getString("endDate");
		String startAmount = jsobject.getString("startAmount");
		String endAmount = jsobject.getString("endAmount");
		String feeType = jsobject.getString("feeType");
		String costType = jsobject.getString("costType");
		String cName = jsobject.getString("cName");
		String ep = jsobject.getString("ep");

		String sql = "select " + 
							"b.CL_NAME AS SHOW_TYPE, " + 
							"c.CL_NAME AS SHOW_CTYPE, " + 
							"d.NAME as SHOW_EP, " + 
							"a.* "
					 + "from CTM_COST a " + 
							"left join CM_COMMLIST b on a.TYPE=b.CL_CODE " + 
							"left join CM_COMMLIST c on a.CTYPE=c.CL_CODE " + 
							"left join APP_LOGIN d on a.ep=d.uid where 1=1";

		// 费用发生时间开始
		if (!"".equals(startDate)) {
			sql += " and DATE>='" + startDate + "'";
		}

		// 费用发生时间结束
		if (!"".equals(endDate)) {
			sql += " and DATE<='" + endDate + "'";
		}

		// 金额开始
		if (!"".equals(startAmount)) {
			sql += " and AMOUNT>=" + startAmount;
		}

		// 金额结束
		if (!"".equals(endAmount)) {
			sql += " and AMOUNT<=" + endAmount;
		}

		// 费用分类
		if (!"".equals(feeType)) {
			sql += " and TYPE='" + feeType + "'";
		}

		// 成本分类
		if (!"".equals(costType)) {
			sql += " and CTYPE='" + costType + "'";
		}

		// 费用名称
		if (!"".equals(cName)) {
			sql += " and CNAME like '%" + cName + "%'";
		}

		// 录入人
		if (!"".equals(ep)) {
			sql += " and c.Name like '%" + ep + "%'";
		}

		sql += " order by a.ID desc";

		DAO dao = new DAO();

		String resultStr = ConstValue.EMPTY_RECORD_NOT_PAGEING;

		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null && v.size() > 0) {
				JSONArray json = JSONArray.fromObject(v);

				resultStr = json.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ConstValue.EMPTY_RECORD_NOT_PAGEING;
		}

		return resultStr;
	}
}
